package cn.kgc.mes.salse.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.salse.domain.ProductSalse;
import cn.kgc.mes.salse.service.IProductSalseService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 销售出库单Controller
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@RestController
@RequestMapping("/salse/salse")
public class ProductSalseController extends BaseController
{
    @Autowired
    private IProductSalseService productSalseService;

    /**
     * 查询销售出库单列表
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSalse productSalse)
    {
        startPage();
        List<ProductSalse> list = productSalseService.selectProductSalseList(productSalse);
        return getDataTable(list);
    }

    /**
     * 导出销售出库单列表
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:export')")
    @Log(title = "销售出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSalse productSalse)
    {
        List<ProductSalse> list = productSalseService.selectProductSalseList(productSalse);
        ExcelUtil<ProductSalse> util = new ExcelUtil<ProductSalse>(ProductSalse.class);
        util.exportExcel(response, list, "销售出库单数据");
    }

    /**
     * 获取销售出库单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:query')")
    @GetMapping(value = "/{salseId}")
    public AjaxResult getInfo(@PathVariable("salseId") Long salseId)
    {
        return success(productSalseService.selectProductSalseBySalseId(salseId));
    }

    /**
     * 新增销售出库单
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:add')")
    @Log(title = "销售出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSalse productSalse)
    {
        return toAjax(productSalseService.insertProductSalse(productSalse));
    }

    /**
     * 修改销售出库单
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:edit')")
    @Log(title = "销售出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSalse productSalse)
    {
        return toAjax(productSalseService.updateProductSalse(productSalse));
    }

    /**
     * 删除销售出库单
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:remove')")
    @Log(title = "销售出库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{salseId}")
    public AjaxResult remove(@PathVariable Long salseId)
    {
        return toAjax(productSalseService.deleteProductSalseBySalseId(salseId));
    }
    /**
     * 出库
     */
//    @PreAuthorize("@ss.hasPermi('salse:salse:out')")
    @Log(title = "销售出库单", businessType = BusinessType.UPDATE)
    @PutMapping("/out/{salseId}") // 路径参数名改为 salseId
    public AjaxResult out(@PathVariable Long salseId) // 与路径参数名一致
    {
        int i = productSalseService.outProductSalseBySalseId(salseId);
        if (i <= 0) {
            // 出库失败：使用AjaxResult.error返回错误信息
            return AjaxResult.error("出库失败，未找到对应记录或出库操作未执行");
        }
        return AjaxResult.success("出库成功");
    }
    /**
     * 更新销售出库单状态
     */
    @PutMapping("/status/{salseId}")
    public AjaxResult updateStatus(@PathVariable Long salseId, @RequestParam String status) {
        // 1. 校验单据存在性
        ProductSalse salse = productSalseService.selectProductSalseBySalseId(salseId);
        if (salse == null) {
            return AjaxResult.error("单据不存在");
        }
        // 2. 更新状态
        salse.setStatus(status);
        int success = productSalseService.updateProductSalse(salse);
        return success>0 ? AjaxResult.success("状态更新成功") : AjaxResult.error("状态更新失败");
    }
}
